home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / DirectX SDK / DXSDK / samples / Multimedia / DirectPlay / Tutorials / Tut02_Host / Host.cpp next >
Encoding:
C/C++ Source or Header  |  2001-10-08  |  8.4 KB  |  276 lines

  1. //----------------------------------------------------------------------------
  2. // File: Host.cpp
  3. //
  4. // Desc: This simple program builds upon the EnumSP.cpp tutorial and adds 
  5. //       creation of an Address Object and hosting a session
  6. //
  7. // Copyright (c) 2000-2001 Microsoft Corp. All rights reserved.
  8. //-----------------------------------------------------------------------------
  9. #define INITGUID
  10. #define _WIN32_DCOM
  11. #include <stdio.h>
  12. #include <conio.h>
  13. #include <dplay8.h>
  14.  
  15.  
  16. //-----------------------------------------------------------------------------
  17. // Global variables
  18. //-----------------------------------------------------------------------------
  19. IDirectPlay8Peer*                   g_pDP = NULL;
  20. IDirectPlay8Address*                g_pDeviceAddress = NULL;
  21.  
  22. // This GUID allows DirectPlay to find other instances of the same game on
  23. // the network.  So it must be unique for every game, and the same for 
  24. // every instance of that game.  // {5e4ab2ee-6a50-4614-807e-c632807b5eb1}
  25. GUID g_guidApp = {0x5e4ab2ee, 0x6a50, 0x4614, {0x80, 0x7e, 0xc6, 0x32, 0x80, 0x7b, 0x5e, 0xb1}};
  26.  
  27.  
  28. //-----------------------------------------------------------------------------
  29. // Function-prototypes
  30. //-----------------------------------------------------------------------------
  31. HRESULT WINAPI DirectPlayMessageHandler(PVOID pvUserContext, DWORD dwMessageId, PVOID pMsgBuffer);
  32. BOOL    IsServiceProviderValid(const GUID* pGuidSP);
  33. HRESULT InitDirectPlay();
  34. HRESULT CreateDeviceAddress();
  35. HRESULT HostSession();
  36. void    CleanupDirectPlay();
  37.  
  38.  
  39. //-----------------------------------------------------------------------------
  40. // Miscellaneous helper functions
  41. //-----------------------------------------------------------------------------
  42. #define SAFE_DELETE(p)          {if(p) {delete (p);     (p)=NULL;}}
  43. #define SAFE_DELETE_ARRAY(p)    {if(p) {delete[] (p);   (p)=NULL;}}
  44. #define SAFE_RELEASE(p)         {if(p) {(p)->Release(); (p)=NULL;}}
  45.  
  46.  
  47.  
  48.  
  49. //-----------------------------------------------------------------------------
  50. // Name: main()
  51. // Desc: Entry point for the application.  
  52. //-----------------------------------------------------------------------------
  53. int main(int argc, char* argv[], char* envp[])
  54. {
  55.     HRESULT hr;
  56.  
  57.     // Init COM so we can use CoCreateInstance
  58.     CoInitializeEx(NULL, COINIT_MULTITHREADED);
  59.  
  60.     // Init the DirectPlay system
  61.     if( FAILED( hr = InitDirectPlay() ) )
  62.     {
  63.         printf("Failed Initializing DirectPlay:  0x%X\n", hr);
  64.         goto LCleanup;
  65.     }
  66.  
  67.     if( FAILED( hr = CreateDeviceAddress() ) )
  68.     {
  69.         printf("Failed CreatingDeviceAddress:  0x%X\n", hr);
  70.         goto LCleanup;
  71.     }
  72.  
  73.     if( FAILED( hr = HostSession() ) )
  74.     {
  75.         printf("Failed Hosting:  0x%X\n", hr);
  76.         goto LCleanup;
  77.     }
  78.  
  79.     // Wait for user action
  80.     printf("Press a key to exit\n");
  81.     _getch();
  82.  
  83. LCleanup:
  84.     CleanupDirectPlay();
  85.  
  86.     // Cleanup COM
  87.     CoUninitialize();
  88.  
  89.     return 0;
  90. }
  91.  
  92.  
  93.  
  94.  
  95. //-----------------------------------------------------------------------------
  96. // Name: InitDirectPlay()
  97. // Desc: Initialize DirectPlay
  98. //-----------------------------------------------------------------------------
  99. HRESULT InitDirectPlay()
  100. {
  101.     HRESULT     hr = S_OK;
  102.  
  103.     // Create the IDirectPlay8Peer Object
  104.     if( FAILED( hr = CoCreateInstance( CLSID_DirectPlay8Peer, NULL, 
  105.                                        CLSCTX_INPROC_SERVER,
  106.                                        IID_IDirectPlay8Peer, 
  107.                                        (LPVOID*) &g_pDP ) ) )
  108.     {
  109.         printf("Failed Creating the IDirectPlay8Peer Object:  0x%X\n", hr);
  110.         goto LCleanup;
  111.     }
  112.  
  113.     // Init DirectPlay
  114.     if( FAILED( hr = g_pDP->Initialize(NULL, DirectPlayMessageHandler, 0 ) ) )
  115.     {
  116.         printf("Failed Initializing DirectPlay:  0x%X\n", hr);
  117.         goto LCleanup;
  118.     }
  119.     
  120.     // Ensure that TCP/IP is a valid Service Provider
  121.     if( FALSE == IsServiceProviderValid(&CLSID_DP8SP_TCPIP ) )
  122.     {
  123.         hr = E_FAIL;
  124.         printf("Failed validating CLSID_DP8SP_TCPIP");
  125.         goto LCleanup;
  126.     }
  127.  
  128. LCleanup:
  129.     return hr;
  130. }
  131.  
  132.  
  133.  
  134.  
  135. //-----------------------------------------------------------------------------
  136. // Name: DirectPlayMessageHandler
  137. // Desc: Handler for DirectPlay messages.  This tutorial doesn't repond to any
  138. //       DirectPlay messages
  139. //-----------------------------------------------------------------------------
  140. HRESULT WINAPI DirectPlayMessageHandler(PVOID pvUserContext, DWORD dwMessageId, PVOID pMsgBuffer)
  141. {
  142.     return S_OK;
  143. }
  144.  
  145.  
  146.  
  147.  
  148. //-----------------------------------------------------------------------------
  149. // Name: IsServiceProviderValid()
  150. // Desc: Return TRUE if the service provider is valid
  151. //-----------------------------------------------------------------------------
  152. BOOL IsServiceProviderValid(const GUID* pGuidSP)
  153. {
  154.     HRESULT                     hr;
  155.     DPN_SERVICE_PROVIDER_INFO*  pdnSPInfo = NULL;
  156.     DWORD                       dwItems = 0;
  157.     DWORD                       dwSize = 0;
  158.  
  159.     hr = g_pDP->EnumServiceProviders( &CLSID_DP8SP_TCPIP, NULL, NULL, 
  160.                                       &dwSize, &dwItems, 0 );
  161.  
  162.     if( hr != DPNERR_BUFFERTOOSMALL )
  163.     {
  164.         printf("Failed Enumerating Service Providers:  0x%x\n", hr);
  165.         goto LCleanup;
  166.     }
  167.  
  168.     pdnSPInfo = (DPN_SERVICE_PROVIDER_INFO*) new BYTE[dwSize];
  169.  
  170.     if( FAILED( hr = g_pDP->EnumServiceProviders( &CLSID_DP8SP_TCPIP, NULL, pdnSPInfo, 
  171.                                                   &dwSize, &dwItems, 0 ) ) )
  172.     {
  173.         printf("Failed Enumerating Service Providers:  0x%x\n", hr);
  174.         goto LCleanup;
  175.     }
  176.  
  177.     // There are no items returned so the requested SP is not available
  178.     if( dwItems == 0)
  179.     {
  180.         hr = E_FAIL;
  181.     }
  182.  
  183. LCleanup:
  184.     SAFE_DELETE_ARRAY(pdnSPInfo);
  185.     
  186.     if( SUCCEEDED(hr) )
  187.         return TRUE;
  188.     else
  189.         return FALSE;
  190. }
  191.  
  192.  
  193.  
  194.  
  195. //-----------------------------------------------------------------------------
  196. // Name: CreateDeviceAddress()
  197. // Desc: Creates a device address
  198. //-----------------------------------------------------------------------------
  199. HRESULT CreateDeviceAddress()
  200. {
  201.     HRESULT         hr = S_OK;
  202.  
  203.     // Create our IDirectPlay8Address Device Address
  204.     if( FAILED( hr = CoCreateInstance( CLSID_DirectPlay8Address, NULL,
  205.                                        CLSCTX_INPROC_SERVER,
  206.                                        IID_IDirectPlay8Address,
  207.                                        (LPVOID*) &g_pDeviceAddress ) ) )
  208.     {
  209.         printf("Failed Creating the IDirectPlay8Address Object:  0x%X\n", hr);
  210.         goto LCleanup;
  211.     }
  212.     
  213.     // Set the SP for our Device Address
  214.     if( FAILED( hr = g_pDeviceAddress->SetSP(&CLSID_DP8SP_TCPIP ) ) )
  215.     {
  216.         printf("Failed Setting the Service Provider:  0x%X\n", hr);
  217.         goto LCleanup;
  218.     }
  219.  
  220. LCleanup:
  221.     return hr;
  222. }
  223.  
  224.  
  225.  
  226.  
  227. //-----------------------------------------------------------------------------
  228. // Name: HostSession()
  229. // Desc: Host a DirectPlay session
  230. //-----------------------------------------------------------------------------
  231. HRESULT HostSession()
  232. {
  233.     HRESULT                 hr = S_OK;
  234.     DPN_APPLICATION_DESC    dpAppDesc;
  235.  
  236.     // Now set up the Application Description
  237.     ZeroMemory(&dpAppDesc, sizeof(DPN_APPLICATION_DESC));
  238.     dpAppDesc.dwSize = sizeof(DPN_APPLICATION_DESC);
  239.     dpAppDesc.guidApplication = g_guidApp;
  240.  
  241.     // We are now ready to host the app
  242.     if( FAILED( hr = g_pDP->Host( &dpAppDesc,            // AppDesc
  243.                                   &g_pDeviceAddress, 1,   // Device Address
  244.                                   NULL, NULL,             // Reserved
  245.                                   NULL,                   // Player Context
  246.                                   0 ) ) )                 // dwFlags
  247.     {
  248.         printf("Failed Hosting:  0x%X\n", hr);
  249.         goto LCleanup;
  250.     }
  251.     else
  252.     {
  253.         printf("Currently Hosting...\n");
  254.     }
  255.  
  256. LCleanup:
  257.     return hr;
  258. }
  259.  
  260.  
  261.  
  262.  
  263. //-----------------------------------------------------------------------------
  264. // Name: CleanupDirectPlay()
  265. // Desc: Cleanup DirectPlay
  266. //-----------------------------------------------------------------------------
  267. void CleanupDirectPlay()
  268. {
  269.     // Cleanup DirectPlay
  270.     if( g_pDP)
  271.         g_pDP->Close(0);
  272.  
  273.     SAFE_RELEASE(g_pDeviceAddress);
  274.     SAFE_RELEASE(g_pDP);
  275. }
  276.